import baozou1_sum
import baozou2_sum
import baozou3_sum
import baozou4_sum
import daishu1_sum
import daishu2_sum
import daishu3_sum
import daishu4_sum
import daishu5_sum
import weiyueyun_sum
import xigua1_sum
import xigua2_sum
import zhangdu1_sum
import zhangdu2_sum
import wangpai_sum
import zhangzhongyun1_sum
import zhangzhongyun2_sum
import tiantianyun1_sum
import tiantianyun2_sum
import zhuishuyun_sum
import weixiaobao_sum
import youshuge_sum
import shenshu_sum
import qire_sum
import yuewei_sum
import haoshuyun_sum
import seshi_sum
import niuniutui1_sum
import niuniutui2_sum
import niuniutui3_sum
import datetime
import threading
import insert_mysql
from openpyxl import Workbook

list1 = []
threads = []


def aa():
    try:
        a = baozou1_sum.baozou1()
        list1.append(a)
    except:
        print("添加 暴走1 数据出错")
    try:
        b = baozou2_sum.baozou2()
        list1.append(b)
    except:
        print("添加 暴走2 数据出错")
    try:
        c = baozou3_sum.baozou3()
        list1.append(c)
    except:
        print("添加 暴走3 数据出错")
    try:
        d = daishu1_sum.daishu1()
        list1.append(d)
    except:
        print("添加 袋鼠1 数据出错")
    try:
        e = daishu2_sum.daishu2()
        list1.append(e)
    except:
        print("添加 袋鼠2 数据出错")


def bb():
    try:
        f = shenshu_sum.shenshu()
        list1.append(f)
    except:
        print("添加 神书 数据出错")
    try:
        g = zhangzhongyun1_sum.zhangzhongyun1()
        list1.append(g)
    except:
        print("添加 掌中云1 数据出错")
    try:
        h = zhangzhongyun2_sum.zhangzhongyun2()
        list1.append(h)
    except:
        print("添加 掌中云2 数据出错")
    try:
        i = tiantianyun1_sum.tiantianyun1_sum()
        list1.append(i)
    except:
        print("添加 天天云1 数据出错")
    try:
        j = tiantianyun2_sum.tiantianyun2_sum()
        list1.append(j)
    except:
        print("添加 天天云2 数据出错")


def cc():
    try:
        k = yuewei_sum.yuewei()
        list1.append(k)
    except:
        print("添加 阅微 数据出错")
    try:
        l = weiyueyun_sum.weiyueyun()
        list1.append(l)
    except:
        print("添加 微阅云 数据出错")
    try:
        m = qire_sum.qire()
        list1.append(m)
    except:
        print("添加 奇热 数据出错")


def dd():
    try:
        n = weixiaobao_sum.weixiaobao()
        list1.append(n)
    except:
        print("添加 微小宝 数据出错")
    try:
        o = daishu3_sum.daishu3()
        list1.append(o)
    except:
        print("添加 袋鼠3 数据出错")
    try:
        p = daishu4_sum.daishu4()
        list1.append(p)
    except:
        print("添加 袋鼠4 数据出错")
    try:
        q = zhuishuyun_sum.zhuishuyun()
        list1.append(q)
    except:
        print("添加 追书云 数据出错")
    try:
        r = youshuge_sum.youshuge()
        list1.append(r)
    except:
        print("添加 有书阁 数据出错")


def ee():
    try:
        s = xigua1_sum.xigua1()
        list1.append(s)
    except:
        print("添加 西瓜1 数据出错")
    try:
        t = xigua2_sum.xigua2()
        list1.append(t)
    except:
        print("添加 西瓜2 数据出错")
    try:
        u = wangpai_sum.wangpai()
        list1.append(u)
    except:
        print("添加 王牌 数据出错")


def ff():
    try:
        v = zhangdu1_sum.zhangdu1()
        list1.append(v)
    except:
        print("添加 掌都1 数据出错")
    try:
        w = zhangdu2_sum.zhangdu2()
        list1.append(w)
    except:
        print("添加 掌都2 数据出错")
    try:
        x = baozou4_sum.baozou4()
        list1.append(x)
    except:
        print("添加 暴走4 数据出错")
    try:
        y = haoshuyun_sum.haoshuyun()
        list1.append(y)
    except:
        print("添加 好书云 数据出错")
    try:
        z = seshi_sum.seshi()
        list1.append(z)
    except:
        print("添加 有影 数据出错")


def gg():
    try:
        aaa = niuniutui1_sum.niuniutui1()
        list1.append(aaa)
    except:
        print("添加 牛牛推1_媚色小说 数据出错")
    try:
        bbb = niuniutui2_sum.niuniutui2()
        list1.append(bbb)
    except:
        print("添加 牛牛推2_魔情书苑 数据出错")
    try:
        ccc = niuniutui3_sum.niuniutui3()
        list1.append(ccc)
    except:
        print("添加 牛牛推3_羞涩书吧 数据出错")
    try:
        ddd = daishu5_sum.daishu5()
        list1.append(ddd)
    except:
        print("添加 袋鼠书城_寒灵小说 数据出错")


try:
    t1 = threading.Thread(target=aa, )
    threads.append(t1)
    t2 = threading.Thread(target=bb, )
    threads.append(t2)
    t3 = threading.Thread(target=cc, )
    threads.append(t3)
    t4 = threading.Thread(target=dd, )
    threads.append(t4)
    t5 = threading.Thread(target=ee, )
    threads.append(t5)
    t6 = threading.Thread(target=ff, )
    threads.append(t6)
    t7 = threading.Thread(target=gg, )
    threads.append(t7)
except Exception:
    print("pass")


def test():
    date_time_list = []
    Amount_share_list = []
    Platform_list = []
    wb = Workbook()
    nowtime = datetime.date.today()
    oldtime = datetime.timedelta(days=1)
    dest_filename = '{0} 各平台收入.xlsx'.format(nowtime - oldtime)
    ws1 = wb.active
    ws1.title = "novel"
    ws1.column_dimensions['A'].width = 14.0
    ws1.column_dimensions['B'].width = 12.0
    ws1.column_dimensions['C'].width = 20.0
    tt = nowtime - oldtime
    incomes = 0

    for i in range(len(list1)):
        date_time = list1[i][0]
        Amount_share = list1[i][1]
        Platform = list1[i][2]
        date_time_list.append(date_time)
        Amount_share_list.append(Amount_share)
        Platform_list.append(Platform)
        if ',' in str(list1[i][1]):
            income = float(str(list1[i][1]).replace(',', ''))
        elif "None" in str(list1[i][1]) or str(list1[i][1]) == "":
            income = 0
        else:
            income = float(list1[i][1])
        incomes += income
    incomes = round(incomes, 2)
    try:
        ret = insert_mysql.select_mysql_spider_amount_share(tt)
        if ret == 1:
            try:
                insert_mysql.Update_mysql_spider_amount_share(tt, Amount_share_list[0], Amount_share_list[1],
                                                              Amount_share_list[2], Amount_share_list[3],
                                                              Amount_share_list[4], Amount_share_list[5],
                                                              Amount_share_list[6], Amount_share_list[7],
                                                              Amount_share_list[8], Amount_share_list[9],
                                                              Amount_share_list[10], Amount_share_list[11],
                                                              Amount_share_list[12], Amount_share_list[13],
                                                              Amount_share_list[14], Amount_share_list[15],
                                                              Amount_share_list[16], Amount_share_list[17],
                                                              Amount_share_list[18], Amount_share_list[19],
                                                              Amount_share_list[20], Amount_share_list[21],
                                                              Amount_share_list[22], Amount_share_list[23],
                                                              Amount_share_list[24], Amount_share_list[25],
                                                              Amount_share_list[26], Amount_share_list[27],
                                                              Amount_share_list[28], Amount_share_list[29],
                                                              incomes)
            except Exception as e:
                print(e)
                print("昨日收益数据更新入库 出错")
        else:
            try:
                insert_mysql.insert_spider_amount_share(tt, Amount_share_list[0], Amount_share_list[1],
                                                        Amount_share_list[2], Amount_share_list[3],
                                                        Amount_share_list[4], Amount_share_list[5],
                                                        Amount_share_list[6], Amount_share_list[7],
                                                        Amount_share_list[8], Amount_share_list[9],
                                                        Amount_share_list[10], Amount_share_list[11],
                                                        Amount_share_list[12], Amount_share_list[13],
                                                        Amount_share_list[14], Amount_share_list[15],
                                                        Amount_share_list[16], Amount_share_list[17],
                                                        Amount_share_list[18], Amount_share_list[19],
                                                        Amount_share_list[20], Amount_share_list[21],
                                                        Amount_share_list[22], Amount_share_list[23],
                                                        Amount_share_list[24], Amount_share_list[25],
                                                        Amount_share_list[26], Amount_share_list[27],
                                                        Amount_share_list[28], Amount_share_list[29],
                                                        incomes)
            except Exception as e:
                print(e)
                print("数据入库 失败")
    except Exception as e:
        print(e)
        print("昨日收益入库失败")

    for row in range(2, len(date_time_list) + 2):
        ws1['A1'] = u"日期"
        ws1['B1'] = u"充值"
        ws1['C1'] = u"平台"
        nowtime = datetime.date.today()
        oldtime = datetime.timedelta(days=1)
        ws1['A{0}'.format(row)] = nowtime - oldtime
        if "," in str(Amount_share_list[row - 2]) or str(Amount_share_list[row - 2]) == "None":
            ws1['B{0}'.format(row)] = Amount_share_list[row - 2]
        else:
            ws1['B{0}'.format(row)] = float(Amount_share_list[row - 2])
        ws1['C{0}'.format(row)] = Platform_list[row - 2]
    wb.save(filename=dest_filename)


if __name__ == '__main__':
    for t in threads:
        t.start()  # t.setDaemon(True)
        print("执行线程{}".format(t))
        t.join()
    test()